#!/bin/bash

if [[ -z ${JIANMU_API_URL} ]]
then
    echo 'Error: JIANMU_API_URL is empty!'
    exit 1
fi

set -e

RAW_RESULT=$(curl ${JIANMU_API_URL})

echo $RAW_RESULT

if [[ -z ${JIANMU_JQ_EXP} ]]
then
    API_RESULT=\"$RAW_RESULT\"
else
    API_RESULT=$(echo $RAW_RESULT | jq ${JIANMU_JQ_EXP})
fi

echo "resultFile:"
echo $API_RESULT
echo -e "{"\"api_result\"" ":${API_RESULT}}"" > /usr/resultFile
cat /usr/resultFile
